import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:sweet_app/bloc/search_bloc.dart';
import 'package:sweet_app/model/user.dart';

const String IP_ADDRESS = "192.168.1.107";

void main() async {
  ResultModel model = await Api.login(phone: "110", password: "121343r");
  print(model);
}

final Dio dio = Dio(Options(baseUrl: "http://$IP_ADDRESS:9091/app"));

class ResultType {
  static const String success = "success";
  static const String error = "error";
}

class ResultModel {
  String type;
  Map<String, dynamic> data;
  String error;

  static ResultModel createError(String error) {
    ResultModel resultModel = ResultModel();
    resultModel.type = ResultType.error;
    resultModel.error = error;
    return resultModel;
  }

  static ResultModel createSuccess(Map<String, dynamic> map) {
    ResultModel resultModel = ResultModel();
    resultModel.type = ResultType.success;
    resultModel.data = map;
    return resultModel;
  }

  ResultModel();

  ResultModel.fromJson(Map<String, dynamic> dataMap) {
    type = dataMap["type"];
    data = dataMap["data"];
    error = dataMap["error"];
  }

  @override
  String toString() {
    return 'ResultModel{type: $type, data: $data, error: $error}';
  }
}

class Api {
  static Future<ResultModel> login({String phone, String password}) async {
//    await Future.delayed(Duration(seconds: 1), () {
//      // todo 模拟耗时
//    });
    try {
      Response<String> response = await dio.post("/login", data: {
        "phone": phone,
        "password": password
      });
      return ResultModel.fromJson(json.decode(response.data));
    } catch (e) {
      print(e);
      return ResultModel.createError("网络错误");
    }
//    return ResultModel.createSuccess(
//        {"token": DateTime.now().millisecondsSinceEpoch.toString()});
  }

  static Future<ResultModel> searchFriend({String phone}) async {
    try {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      final token = prefs.getString("token");
      Response<String> response = await dio
          .post("/searchFriend", data: {"token": token, "phone": phone});
      return ResultModel.fromJson(json.decode(response.data));
    } catch (e) {
      print(e);
      return ResultModel.createError("网络错误");
    }
  }

  static Future<ResultModel> register({String phone, String name, String password}) async {
    try {
      Response<String> response = await dio
          .post("/register", data: {"phone": phone, "name": phone, "password": password});
      return ResultModel.fromJson(json.decode(response.data));
    } catch (e) {
      print(e);
      return ResultModel.createError("网络错误");
    }
  }

  static Future<ResultModel> addFriend(User user) async {
    try {
      print("addFriend: $user");
      SharedPreferences prefs = await SharedPreferences.getInstance();
      final token = prefs.getString("token");
      Response<String> response = await dio.post("/addFriend",
          data: {"token": token, "fid": user.id});
      print(response.data);
      return ResultModel.fromJson(json.decode(response.data));
    } catch (e) {
      print(e);
      return ResultModel.createError("网络错误");
    }
  }
}
